var arId;
$(function(){
	var editor;
	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="content"]', {
			uploadJson : 'http://localhost:8090/imageUpload',
			fileManagerJson : 'http://localhost:8090/fileManager',
			allowFileManager : true
		});
		K('input[name=getHtml]').click(function(e) {
			alert(editor.html());
		});
		K('input[name=isEmpty]').click(function(e) {
			alert(editor.isEmpty());
		});
		K('input[name=getText]').click(function(e) {
			alert(editor.text());
		});
		K('input[name=selectedHtml]').click(function(e) {
			alert(editor.selectedHtml());
		});
		K('input[name=setHtml]').click(function(e) {
			editor.html('<h3>Hello KindEditor</h3>');
		});
		K('input[name=setText]').click(function(e) {
			editor.text('<h3>Hello KindEditor</h3>');
		});
		K('input[name=insertHtml]').click(function(e) {
			editor.insertHtml('<strong>插入HTML</strong>');
		});
		K('input[name=appendHtml]').click(function(e) {
			editor.appendHtml('<strong>添加HTML</strong>');
		});
		K('input[name=clear]').click(function(e) {
			editor.html('');
		});
        $.ajax({
            url : "getArticleByLetterId",
            type : "GET",
            data :{
                letterId : letterId
            },
            success : function (result) {
                var article = result.extend.article;
                arId = article.articleId;
                var con = '' + article.content;
                editor.html(con);
                $(".panel-primary").eq(0).find("textarea").val(article.title);
                $(".panel-primary").eq(1).find("textarea").val(article.simContent);
                var con1 = '<img src="/' + article.articlePicture + '">';
                editor2.html(con1);

            }
        })

	});
	var editor2;
	KindEditor.ready(function(K) {
		editor2 = K.create('textarea[name="content2"]', {
            uploadJson : 'http://localhost:8090/imageUpload',
            fileManagerJson : 'http://localhost:8090/fileManager',
			allowFileManager : true,
			items : ["image"],
			//readonlyMode:true
		});
    });

	$("button[name=clear]").click(function () {
		editor.html('');
		editor2.html('');
    });
	$("button[name=sub]").click(function() {
		var userId = $(".user-nav input").val();
		var content = editor.html();
		var title = $(".panel-primary").eq(0).find("textarea").val();
		var simcontent = $(".panel-primary").eq(1).find("textarea").val();
		var date = new Date();//获得时间对象
		var tag = $(".panel-primary").eq(2).find("textarea").val();
		var articleDate =  date.getFullYear()+"-"+(date.getMonth() + 1) + "-" + date.getDate();
		var articlePicture =  editor2.html().substring(11,(editor2.html()+"").length-11);
		if(content.length < 1){
			alert("正文应大于1字");
			return;
		}else if(title.length > 50 || title.length < 1){
			alert("title应在1~50字之间");
			return;
		}else if(simcontent.length > 80 || simcontent.length < 1){
			alert("文章简述应在1~80字之间");
			return;
		}
		updataArticle(userId,content,title,simcontent,articleDate,articlePicture,tag);
		//var tag = $(".panel-primary").eq(2).find("li .on").next("input").val();
	});
});

function updataArticle(userId,content,title,simcontent,articleDate,articlePicture,tag) {

	var data  = {
		articleId:arId,
        userId : userId,
        content : content,
        simContent : simcontent,
        title : title,
        articleDate : articleDate,
        articlePicture : articlePicture,
    };
    var a = $.ajax("updateArticle", {
        data : data
    })
    a.done(function (result) {
        if (result.code == 100) {
            alert("发表成功");
            window.location.href='home';
        } else {
            alert("发表失败")
        }
    });

}

function sub() {
	var file = $("#picID").val();
	if (file == "") {
		alert("请选择图片~");
	} else {
		$("#form1").submit();
	}
}